﻿/*
 * Copyright 2012 Rene Prost, Registrite ja Infosüsteemide Keskus 
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
using System;
using System.IO;
using System.Text;
using System.Web;
using NUnit.Framework;
 
using Xtee.Core.Adapter.Service;
using Xtee.Core.Mime;
using HttpContextWrapper = Xtee.Core.Adapter.HttpContextWrapper;

namespace Xtee.Core.Test.Adapter
{
    [TestFixture]
    public class HttpContextWrapperTests : TestBase
    {
        [Test]
        public void OutputStreamShouldHttpConnectStream()
        {
            HttpContext context =  base.GetTestContext("Request Data");
            HttpContextWrapper wrapper = GetHttpContextWrapper(context);
           
        }

        [Test]
        public void OutputStreamShouldHBeMemoryStream()
        {
            HttpContext context = base.GetTestContext("Request Data");
            HttpContextWrapper wrapper = GetHttpContextWrapper(context);
           
            Assert.That(wrapper.ResponseStream is MemoryStream);
        }

        private static HttpContextWrapper GetHttpContextWrapper(HttpContext context)
        {
            return new HttpContextWrapper(context, Configurator.GetConfig());
        }


        [Test]
        public void SaveShouldSucced()
        {
            HttpContext context = base.GetTestContext("Request Data","text/sometype");
     
            Console.WriteLine(context.Request.ContentType);

            HttpContextWrapper wrapper = GetHttpContextWrapper(context);
         
            Assert.That(wrapper.ResponseStream is MemoryStream);
            wrapper.ResponseHeaders.Add("Content-Disposion","attachment");
            wrapper.ResponseStream.Write(Encoding.UTF8.GetBytes("Response data"), 0, Encoding.UTF8.GetBytes("Response data").Length);
              string pathinput =  Environment.GetEnvironmentVariable("TEMP")+"\\wrapperInput.txt";
              string pathoutput =  Environment.GetEnvironmentVariable("TEMP")+"\\wrapperOutput.txt";
            wrapper.SaveRequest(pathinput);
            wrapper.SaveResponse(pathoutput);
            Assert.That(File.Exists(pathinput));
            Assert.That(File.Exists(pathoutput));
            const string requestOut =
@"Content-Type: text/sometype

Request Data";
            const string responseOut = 
@"Content-Disposion: attachment

Response data";            
            Assert.That(File.ReadAllText(pathinput), Is.EqualTo(requestOut));
            Assert.That(File.ReadAllText(pathoutput), Is.EqualTo(responseOut));
        }

        [Test]
        public void  TestThatAfterFlushResponseHeadersAreSetAndOutputStreamIsFilled_Buffered()
        {
            HttpContext context = base.GetTestContext("mäh");

            HttpContextWrapper wrapper = GetHttpContextWrapper(context);
            
         
  
            wrapper.ResponseHeaders.Add("Content-Type", "text/somegrazytype");
            wrapper.ResponseStream.Write(Encoding.UTF8.GetBytes("tsurr"),0,5);
            wrapper.FlushResponse();
            context.Response.Flush();

            Assert.That(base.output.ToString(), Is.EqualTo("tsurr"));
            Assert.That(new ContentType(context.Response.ContentType).MediaType,Is.EqualTo("text/somegrazytype"));
        }
        [Test]
        public void TestThatAfterFlushResponseHeadersAreSetAndOutputStreamIsFilled_UnBufferd()
        {
            HttpContext context = base.GetTestContext("mäh");

            HttpContextWrapper wrapper = GetHttpContextWrapper(context);

           
          
            wrapper.ResponseHeaders.Add("Content-Type", "text/somegrazytype");
            wrapper.ResponseStream.Write(Encoding.UTF8.GetBytes("tsurr"), 0, 5);
            wrapper.FlushResponse();
            context.Response.Flush();
            Assert.That(base.output.ToString(), Is.EqualTo("tsurr"));
            Assert.That(new ContentType(context.Response.ContentType).MediaType, Is.EqualTo("text/somegrazytype"));
        }
    }
}
